#!/bin/bash

set -e
set -x

bypy download apache-zookeeper-3.7.2-bin.tar.gz
sh render.sh

iplist_path=config
i=0
while read -r line
do
    case $line in
        *'#'*)
            continue
        ;;
        *'end'*)
            break
        ;;
        *)
            IFS=',' read -ra parts <<< "$line"
        ;;
    esac
    
    pass="${parts[0]}" 
    address="${parts[1]}"
    hostname="${parts[2]}"
    
    if [ $address ];then
        sshpass -p$pass scp apache-zookeeper-3.7.2-bin.tar.gz $address:/opt/
        sshpass -p$pass ssh -n $address tar xf /opt/apache-zookeeper-3.7.2-bin.tar.gz -C /opt
        sshpass -p$pass ssh -n $address ln -fs /opt/apache-zookeeper-3.7.2-bin /opt/zookeeper
        sshpass -p$pass ssh -n $address mkdir -p /opt/zookeeper/data
        sshpass -p$pass scp zoo.cfg $address:/opt/zookeeper/conf/
        sshpass -p$pass ssh -n $address "echo $i > /opt/zookeeper/data/myid"
        sshpass -p$pass scp zookeeper.service $address:/usr/lib/systemd/system/
        sshpass -p$pass ssh -n $address systemctl daemon-reload
        sshpass -p$pass ssh -n $address systemctl enable --now zookeeper
        let i=i+1
    fi
    
done < $iplist_path

set +x
set +e